<?php
/**
 * Zend Framework Contribution
 *
 * LICENSE
 *
 * This source file is subject to the new BSD license that is bundled
 * with this package in the file LICENSE.txt.
 * It is also available through the world-wide-web at this URL:
 * http://framework.zend.com/license/new-bsd
 * If you did not receive a copy of the license and are unable to
 * obtain it through the world-wide-web, please send an email
 * to license@zend.com so we can send you a copy immediately.
 *
 * @category   Zend
 * @package    Zend_Mwb
 * @copyright  Copyright (c) 2005-2011 Zend Technologies USA Inc. (http://www.zend.com)
 * @license    http://framework.zend.com/license/new-bsd     New BSD License
 * @version    1.0 Column.php 3 mai 2012 Sergio DE VASCONCELOS
 */

require_once('Zend/Mwb/Dom/Abstract.php');

/**
 * MySQL Workbench document
 *
 * @category   Zend
 * @package    Zend_Mwb
 */
class Zend_Mwb_Dom_Column extends Zend_Mwb_Dom_Abstract
{
	protected $name;
	protected $type;
	
	public function getName() {
		if (is_null($this->name)) {
			$this->name = $this->loadName();
		}
		return $this->name;
	}
	public function setName($name) { }
	
	public function getType() {
		if (is_null($this->type)) {
			$this->type = $this->loadType();
		}
		return $this->type;
	}
	public function setType($type) {
		$this->type = $type;
		return $this;
	}
	
	public function parse() {
		$this->xpath = new DOMXPath($this->node->ownerDocument);
		
		$this->name    = $this->loadName();
	}
	/**
	 * @param DOMElement $dom
	 * @return 
	 */
	protected function loadName() {
    	$element = $this->xpath->query("value[@key='name']", $this->node)->item(0);
    	if (isset($element)) {
        	return $element->textContent;
        } else {
        	require_once 'Zend/Mwb/Exception.php';
        	throw new Zend_Mwb_Exception('Dom_Exception_Null(Column)');
        }
	}
	/**
	 * @param String $name
	 * @param DOMElement $dom
	 * @return 
	 */
	protected function saveName($name, DOMElement $dom=null) {
    	$element = $this->xpath->query("value[@key='name']", $this->node)->item(0);
        if (isset($element)) {
        	$element->textContent = $name;
        }// FIXME else create/lazyload
	}
	
	protected function loadType() {
    	$element = $this->xpath->query("link[@key='simpleType']", $this->node)->item(0);
        if (isset($element)) {
        	$type = $element->textContent;
        }
		return $type;
	}
	
	public function create() {
$xml = <<<XXX
                    <value type="object" struct-name="db.mysql.Column" id="{3F8D23CB-4E6D-4DC2-9EDA-2368A9316383}" struct-checksum="0x783b5183">
                      <value type="int" key="autoIncrement">1</value>
                      <value type="string" key="characterSetName"></value>
                      <value _ptr_="0x5e23450" type="list" content-type="object" content-struct-name="db.CheckConstraint" key="checks"/>
                      <value type="string" key="collationName"></value>
                      <value type="string" key="datatypeExplicitParams"></value>
                      <value type="string" key="defaultValue"></value>
                      <value type="int" key="defaultValueIsNull">0</value>
                      <value _ptr_="0x5e234b0" type="list" content-type="string" key="flags"/>
                      <value type="int" key="isNotNull">1</value>
                      <value type="int" key="length">-1</value>
                      <value type="int" key="precision">10</value>
                      <value type="int" key="scale">-1</value>
                      <link type="object" struct-name="db.SimpleDatatype" key="simpleType">com.mysql.rdbms.mysql.datatype.int</link>
                      <value type="string" key="comment"></value>
                      <value type="string" key="name">id_alias</value>
                      <value type="string" key="oldName">id_alias</value>
                      <link type="object" struct-name="GrtObject" key="owner">{CEF23D4D-30A1-4EF0-AF4C-F0B049E4EF42}</link>
                    </value>
XXX;

	}
}
